"""
爬取豆瓣电影Top250详情
"""
import random
import time

import bs4
import requests
import openpyxl


def fetch_movie_detail(detail_url):
    resp = requests.get(
        url=detail_url,
        headers={
            'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36'
        }
    )
    document = bs4.BeautifulSoup(resp.text, 'html.parser')
    gener_spans = document.select('span[property="v:genre"]')
    gener = ' / '.join([gener_span.text for gener_span in gener_spans])
    country_span = document.select('span.pl')[4]
    country = country_span.next_sibling.strip()
    language_span = document.select('span.pl')[5]
    language = language_span.next_sibling.strip()
    duration = document.select_one('span[property="v:runtime"]').attrs['content']
    return gener, country, language, duration


def main():
    wb = openpyxl.Workbook()
    sheet = wb.create_sheet('Top250')
    col_names = ('排名', '名称', '评分', '类型', '制片国家', '语言', '时长')
    for index, name in enumerate(col_names):
        sheet.cell(1, index + 1, name)
    rank = 0
    for page in range(10):
        resp = requests.get(
            url=f'https://movie.douban.com/top250?start={page * 25}',
            headers={
                'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36'
            }
        )
        # BeautifulSoup对象代表了整个页面（文档），通过它可以获取到页面上所有内容
        document = bs4.BeautifulSoup(resp.text, 'html.parser')
        info_divs = document.select('div.info')
        for info_div in info_divs:
            rank += 1
            anchor = info_div.select_one('div.hd>a')
            detail_url = anchor.attrs['href']
            title = anchor.select_one('span.title').text
            score = info_div.select_one('div.bd>div.star>span.rating_num').text
            # 用一个列表保存一部电影所有的信息
            movie_details = [rank, title, score]
            movie_details += fetch_movie_detail(detail_url)
            print(movie_details)
            # 将列表中的信息写入Excel文件表单的单元格中
            for index, item in enumerate(movie_details):
                sheet.cell(rank + 1, index + 1, item)
        time.sleep(random.random() * 3 + 3)
    wb.save('豆瓣电影.xlsx')


if __name__ == '__main__':
    main()
